import { BASE_URL } from './config';
const whiteList = ['auth/getWxOpenid','user/storeUser']; 
import utils from './utils';
// import store from '../store';
class FunFetch {
	constructor() {
	    this.token = "";
	}
    async request(url, params, method = 'GET') {
		 if (!whiteList.includes(url)) {
			const token = uni.getStorageSync('token')
			this.token = token;
		 }
        return new Promise((resolve, reject) => {
            uni.request({
                url: BASE_URL + url,
                data: params,
                header: {
                    token: this.token
                },
                method,
                dataType: 'json',
                responseType: 'text',
                success: function (res) {
					resolve(res.data);
                    uni.hideLoading();
                },
                fail: function (res) {
                    reject(res);
                    uni.showToast({
                        title: res.message || '请求错误',
                        icon: 'none',
                        duration: 1500
                    })
                    uni.hideLoading();
                },
                complete: function () {},
            });
        })
    }
    get(url, params) {
        return this.request(url, params, 'GET');
    }
    // post
    post(url, params) {
        return this.request(url, params, 'POST');
    }
    //获取token
    getToken() {
        let token = uni.getStorageSync('token');
        if (token) {
            return Promise.resolve(token);
        } else {
            return new Promise((resolve, reject) => {
                uni.login({
                    success: ({code}) => {
                         // 发送 res.code 到后台换取 openId, sessionKey, unionId
                        this.post('auth/getWxOpenid', {code: code}).then(res => {
							    let userInfo = uni.getStorageSync("user")
								console.log(userInfo)
							    this.post('user/storeUser', {unionid:res.unionid, 
								                              openid: res.data.openid, 
															  nickname: userInfo.nickName, 
															  url: userInfo.avatarUrl,
															  sex: userInfo.gender,
															  encrypted_data: userInfo.encryptedData,
															  iv: userInfo.iv}).then(tokenRes => {
							        token = tokenRes.data.token;
							        uni.setStorageSync('token', tokenRes.data.token);
							        resolve(token);
							}).catch((err) => {
							    console.log('获取用户信息失败：：：', err);
							    // store.commit('SET_ISAUTH', false);
							})
                        });
                    },
                    fail(err) {
                        reject(err);
                    }
                });
            });
        };
    }
	
   // wx login
    login() {
        return new Promise((resolve, reject) => {
            uni.login({
                success: (loginRes) => {
                    resolve(loginRes);
                },
                fail(err) {
                    reject(err);
                }
            });
        });
    }
};

export default new FunFetch();